import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:hxdj/common/colors/my_colors.dart';
import 'package:hxdj/common/components/my_image.dart';
import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/components/my_alert.dart';
import 'package:hxdj/common/components/my_appbar.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/common/my_constant.dart';
import 'package:hxdj/common/my_global.dart';
import 'package:hxdj/common/routers/navigator.dart';
import 'package:hxdj/login/login_page.dart';
import 'package:hxdj/mine/cancel_account/cancel_account_page.dart';
import 'package:hxdj/mine/safe_account/safe_account_viewmodel.dart';

//账号安全
class SafeAccountPage extends StatefulWidget {
  static String routeName = "/safeAccount";

  const SafeAccountPage({super.key});

  @override
  State<SafeAccountPage> createState() => _SafeAccountPageState();
}

class _SafeAccountPageState extends State<SafeAccountPage> {
  //viewModel
  SafeAccountViewModel? safeAccountViewModel;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BasePage<SafeAccountViewModel>(
      onModelReady: (model) async {
        // 页面初始化完成后的回调
        safeAccountViewModel = model;

        //获取缓存的大小
        await safeAccountViewModel!.formatSize();
      },
      viewModel: SafeAccountViewModel(),
      builder: (BuildContext context, SafeAccountViewModel model, Widget? child) {
        // cancelAccountViewModel = model;
        return Scaffold(
            backgroundColor: const Color(0xFFF5F5F5),
            appBar: const MyAppBar(
              title: '账号安全',
            ),
            body: Container(
                margin: const EdgeInsets.only(top: 10),
                color: MyColours.bgColor,
                child: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
                  GestureDetector(
                    onTap: () {
                      MyAlertManager.showAlert(AlertType.iosMode, context, "您确定要清除缓存吗？", "", onSure: () {
                        safeAccountViewModel!.clearCache();
                      }, close: "再想想", sure: "确定清除");
                    },
                    child: Container(
                        margin: const EdgeInsets.fromLTRB(15, 15, 15, 0),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(6.5),
                        ),
                        child: Container(
                          padding: const EdgeInsets.fromLTRB(10, 15, 10, 15),
                          child: Row(children: [
                            const Text("清除缓存", style: TextStyle(fontSize: 14, color: MyColours.textBlack)),
                            const Spacer(),
                            Text(model.cacheSize,
                                style: const TextStyle(
                                    fontSize: 13, color: MyColours.textBlack, fontWeight: FontWeight.w400)),
                            const Padding(
                              padding: EdgeInsets.only(left: 10),
                              child: MyImage(imageName: 'ic_arrow_right', width: 15),
                            ),
                          ]),
                        )),
                  ),
                  GestureDetector(
                    onTap: () {
                      //注销账号
                      NavigatorUtils.push(context, CancelAccountPage.routeName);
                    },
                    child: Container(
                        margin: const EdgeInsets.fromLTRB(15, 15, 15, 0),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(6.5),
                        ),
                        child: Container(
                          padding: const EdgeInsets.fromLTRB(10, 15, 10, 15),
                          child: const Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                            Text("注销账号", style: TextStyle(fontSize: 14, color: MyColours.textBlack)),
                            MyImage(imageName: 'ic_arrow_right', width: 15),
                          ]),
                        )),
                  ),
                  const Spacer(),
                  Container(
                      margin: const EdgeInsets.fromLTRB(15, 0, 15, 60),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(6.5),
                      ),
                      child: GestureDetector(
                        onTap: () {
                          MyAlertManager.showAlert(AlertType.iosMode, context, "您确定要退出登录吗？", "", onSure: () {
                            safeAccountViewModel!.logout();
                            CommonPlugin.sharedPreferences!.setBool(MyConstant.isLogin, false);
                            
                        


                            MyGlobalViewModel globalViewModel = MyGlobalViewModel.getGlobalViewModel(context);
                            globalViewModel.setIndex(0);
                            //跳转到主页面
                            NavigatorUtils.pushParams(context, LoginPage.routeName,
                                clearStack: true, replace: true, transition: TransitionType.fadeIn);
                          }, close: "再想想", sure: "确定退出");
                        },
                        child: Container(
                          padding: const EdgeInsets.fromLTRB(8.5, 15, 5, 15),
                          child: const Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                            Text("退出登录",
                                style:
                                    TextStyle(fontSize: 15, color: MyColours.textBlack, fontWeight: FontWeight.w500)),
                          ]),
                        ),
                      )),
                ])));
      },
    );
  }
}
